# coding=UTF-8
import imageio
import os
import sys
# import moviepy.editor as mp
import moviepy.video.io.ImageSequenceClip
from PIL import Image
import cv2
# from gtts import gTTS
# from io import BytesIO
# import pyttsx3

def turn_base(in_path, in_path_2, in_path_5, in_path_6, in_path_7, in_path_8, in_path_9, out_path):
	files = os.listdir(in_path)
	for i in range(len(files)):
		file_name = in_path + files[i]
		img = Image.open(file_name)
		file_name_2 = in_path_2 + files[i]
		img_2 = Image.open(file_name_2)
		file_name_5 = in_path_5 + files[i]
		img_5 = Image.open(file_name_5)
		file_name_6 = in_path_6 + files[i]
		img_6 = Image.open(file_name_6)
		file_name_7 = in_path_7 + files[i]
		img_7 = Image.open(file_name_7)
		file_name_8 = in_path_8 + files[i]
		img_8 = Image.open(file_name_8)
		file_name_9 = in_path_9 + files[i]
		img_9 = Image.open(file_name_9)
		size_img = img.size
		print(file_name)
		region_1 = img.crop((0, int(size_img[1] / 2 * 0.00), size_img[0], int(size_img[1] / 2))) # 左，上，右，下，0.04，越小顶部空白越多
		region_2 = img.crop((0, int(size_img[1] / 2), size_img[0], size_img[1] - int(size_img[1] / 2 * 0.09)))
		# 前4名生
		target = Image.new('RGB', (int(size_img[0] * 3.0), int(size_img[1] * 1.1)), (255, 255, 255)) # 拼接前需要写拼接完成后的图片大小
		# target.paste(region_2, (size_img[0] - int(size_img[1] * 0.02), int(size_img[1] / 2 * 0.09), \
		#              size_img[0] * 2 - int(size_img[1] * 0.02), int(size_img[1] / 2))) # 左，上，右，下
		target.paste(region_2, (size_img[0] - int(size_img[1] * 0.02), int(size_img[1] / 2 * 0.09))) # 左，上，右，下
		# target.paste(region_1, (0 + int(size_img[1] * 0.02), 0, size_img[0] + int(size_img[1] * 0.02), region_1.size[1])) # 左，上，右，下
		# 神赐之脑
		target.paste(img_2, (int(size_img[0] * 2.0), 
		             int(size_img[1] * 0.8)) ) # 左，上，右，下
		# 刻印调用频率图
		target.paste(img_5, (int(size_img[0] * 1.0), \
			         int(size_img[1] * 0.48)) )# 左，上，右，下
		# 各刻印到刻印中心距离图
		target.paste(img_6, (0, \
			         int(size_img[1] * 0.48)) )# 左，上，右，下
		# 绘制获得神赐之脑生的素
		target.paste(img_7, (0, \
			         int(size_img[1] * 0.9)) )# 左，上，右，下
		# 绘制回路
		target.paste(img_8, (int(size_img[0] * 2.0), \
			         int(size_img[1] * 0.02)) )# 左，上，右，下
		# 绘制路径出现次数
		target.paste(img_9, (int(size_img[0] * 2.0), \
			         int(size_img[1] * 0.25)) )# 左，上，右，下
		# 世界地图
		target.paste(region_1, (0 + int(size_img[1] * 0.02), int(size_img[1] * 0.01))) # 左，上，右，下
		target.save(out_path + files[i], quality = 99)

def turn_arrival(in_path, in_path_2, in_path_3, in_path_4, out_path):
	files = os.listdir(in_path)
	for i in range(len(files)):
		file_name = in_path + files[i]
		img = Image.open(file_name)
		file_name_2 = in_path_2 + files[i] # 神赐之脑画面
		img_2 = Image.open(file_name_2)
		size_img = img.size
		print(file_name)
		base = size_img[1] # 设置基准 4600
		region_1 = img.crop((0, int(size_img[1] / 2 * 0.04), size_img[0], int(size_img[1] / 2))) # 左，上，右，下，0.04，越小顶部空白越多
		region_2 = img.crop((0, int(size_img[1] / 2), size_img[0], size_img[1]))
		target = Image.new('RGB', (int(size_img[0] * 2), int(size_img[1] / 2)), (255, 255, 255)) # 拼接前需要写拼接完成后的图片大小
		target.paste(region_2, (size_img[0] - int(size_img[1] * 0.02), 0, \
		             size_img[0] * 2 - int(size_img[1] * 0.02), region_2.size[1])) # 左，上，右，下
		target.paste(region_1, (0 + int(size_img[1] * 0.02), 0, size_img[0] + int(size_img[1] * 0.02), region_1.size[1])) # 左，上，右，下
		# 绘制神赐之脑
		target.paste(img_2, (target.size[0] - img_2.size[0], target.size[1] - img_2.size[1] - int(target.size[1] * 0.14), \
		                     target.size[0], target.size[1] - int(target.size[1] * 0.14))) # 左，上，右，下
		# 绘制生使用的摄像头画面
		file_name_3 = in_path_3 + files[i] # 生使用的摄像头画面
		img_3 = cv2.imread(file_name_3)
		img_3 = cv2.resize(img_3, (int(base / 9.0), int(base / 9.0)), interpolation = cv2.INTER_LANCZOS4) # 先更改尺寸
		cv2.imwrite(file_name_3, img_3)
		img_3 = Image.open(file_name_3)
		target.paste(img_3, (int(target.size[0] * 0.635) - img_3.size[0], target.size[1] - img_3.size[1], \
		                     int(target.size[0] * 0.635), target.size[1])) # 左，上，右，下
		# 绘制生使用的摄像头画面
		file_name_4 = in_path_4 + files[i] # 生使用的摄像头画面
		size_move = float(base) / 4600.0
		img_4 = cv2.imread(file_name_4)
		img_4 = cv2.resize(img_4, (int(size_move * len(img_4[0])), int(size_move * len(img_4))), interpolation = cv2.INTER_LANCZOS4) # 先更改尺寸
		cv2.imwrite(file_name_4, img_4)
		img_4 = Image.open(file_name_4)
		target.paste(img_4, (int(target.size[0] * 0.83) - img_4.size[0], target.size[1] - img_4.size[1], \
		                     int(target.size[0] * 0.83), target.size[1])) # 左，上，右，下
		# 保存结果
		target.save(out_path + files[i], quality = 99)

def png2mp4(in_path, out_filename):
	image_files = [in_path + img for img in os.listdir(in_path) if img.endswith(".png")]
	clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(image_files, fps = 20.0)
	clip.write_videofile(out_filename + '.mp4', bitrate='10000k')

